package com.tuanzi.loan.business.service.family.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.entity.family.LoanLargeAmountNewPartnerInfo;
import com.tuanzi.loan.business.entity.family.StatLargePageDetail;
import com.tuanzi.loan.business.repository.family.StatLargePageDetailRepository;
import com.tuanzi.loan.business.service.family.LoanLargeAmountNewPartnerInfoService;
import com.tuanzi.loan.business.service.family.StatLargePageDetailService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.largeamount.LoanStatLargePageDetailRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LoanStatLargePageDetailResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * User: liangZongXi
 * Create time: 15:37 2019/5/27
 * Description:
 */

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class StatLargePageDetailServiceImpl extends CommonServiceImpl<StatLargePageDetail, Long> implements StatLargePageDetailService {
    @Autowired
    private StatLargePageDetailRepository statLargePageDetailRepository;
    @Autowired
    private LoanLargeAmountNewPartnerInfoService loanLargeAmountNewPartnerInfoService;


    @Override
    public Page<LoanStatLargePageDetailResponse> list(PageSearcher<LoanStatLargePageDetailRequest> pageSearcher) {
        return statLargePageDetailRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    protected JpaRepository<StatLargePageDetail, Long> getRepository() {
        return statLargePageDetailRepository;
    }

    @Override
    public Page<LoanStatLargePageDetailResponse> listChannelNdIn(PageSearcher<LoanStatLargePageDetailRequest> pageSearcher) {

        LoginUser loginUser = CurrentUser.get();
        Assert.notNull(loginUser, "无法获取用户账号");
        String account = loginUser.getAccount();
        LoanLargeAmountNewPartnerInfo allByPartnerAccount = loanLargeAmountNewPartnerInfoService.findAllByPartnerAccount(account);
        String[] channelNds = null;
        if(null != allByPartnerAccount){
            String channelNdInfo = allByPartnerAccount.getChannelNdInfo();
            if(StringUtils.isNotBlank(channelNdInfo)){
                channelNds = channelNdInfo.split(",");
            }
        }

        if(null == channelNds || channelNds.length == 0){
            return new PageImpl<>(Lists.newArrayList());
        }
        return statLargePageDetailRepository.listChannelNdIn(pageSearcher.getSearchCondition(), pageSearcher.getPageable(), channelNds);
    }
}
